package com.kgc.xsProvider.service.ServiceImpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.xs.pojo.SalesQuotes;
import com.kgc.xs.pojo.SalesQuotesDetail;
import com.kgc.xs.remote.SalesQuotesRemote;
import com.kgc.xsProvider.dao.SalesQuotesMapper;
import com.kgc.xsProvider.service.SalesQuotesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class SalesQuotesServiceImpl implements SalesQuotesService {

    private Logger  logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private SalesQuotesMapper salesQuotesMapper;


    @Override
    public ResponseMsg selectSalesQuotesByPage( Map<String, Object> map) {
        logger.info("SalesQuotesServiceImpl  selectSalesQuotesByPage params{}",map);
        ResponseMsg responseMsg=new ResponseMsg();
        PageInfo<SalesQuotes> pageInfo=new PageInfo<SalesQuotes>();
        pageInfo.setPageNo(map.get("pageNo")==null?1:Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize")==null?10:Integer.parseInt(map.get("pageSize").toString()));
        Page page= PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        List<SalesQuotes> salesQuotesList=salesQuotesMapper.selectSalesQuotesByPage(map);
        pageInfo.setList(salesQuotesList);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("SalesQuotesServiceImpl selectSalesQuotesByPage {}",responseMsg);
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg insertSalesQuotes(Map<String, Object> map) {
        logger.info("SalesQuotesServiceImpl  insertSalesQuotes params{}",map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();
        SalesQuotes salesQuote=mapper.convertValue(map.get("salesQuote"), SalesQuotes.class);
        List<?> rawDetails = (List<?>) map.get("details");
        List<SalesQuotesDetail> details=mapper.convertValue(rawDetails,new TypeReference<List<SalesQuotesDetail>>(){});
        int result1 = salesQuotesMapper.insertSalesQuotes(salesQuote);
        try{
            for (SalesQuotesDetail detail : details) {
                salesQuotesMapper.insertSalesQuotesDetail(detail);
            }
        }catch (Exception e){
            e.printStackTrace();
            responseMsg.setCode("201");
            return responseMsg;
        }
        if(result1>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.info("SalesQuotesServiceImpl  insertSalesQuotes {}",responseMsg);
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg updateSalesQuotes(Map<String, Object> map) {
        logger.info("SalesQuotesServiceImpl  updateSalesQuotes params{}",map.toString());
        deleteSalesQuotes(map);
        ResponseMsg responseMsg=insertSalesQuotes(map);
        logger.debug("SalesQuotesServiceImpl updateSalesQuotes {}",responseMsg);
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg deleteSalesQuotes(Map<String, Object> map) {
        logger.info("SalesQuotesServiceImpl  deleteSalesQuotes params{}",map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        int result1=salesQuotesMapper.deleteSalesQuotes(map);
        int result2=salesQuotesMapper.deleteSalesQuotesDetail(map);
        if(result1>0&&result2>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesQuotesServiceImpl deleteSalesQuotes {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesQuotesDetailByPage(Map<String, Object> map) {
        logger.info("SalesQuotesServiceImpl  selectSalesQuotesDetailByPage params{}",map);
        ResponseMsg responseMsg=new ResponseMsg();
        List<SalesQuotesDetail> SalesQuotesDetailList=salesQuotesMapper.selectSalesQuotesDetailByPage(map);
        responseMsg.setData(SalesQuotesDetailList);
        logger.debug("SalesQuotesServiceImpl selectSalesQuotesDetailByPage {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg insertSalesQuotesDetail( SalesQuotesDetail salesQuotesDetail) {
        logger.info("SalesQuotesServiceImpl insertSalesQuotesDetail params "+salesQuotesDetail);
        ResponseMsg responseMsg=new ResponseMsg();
        int result=salesQuotesMapper.insertSalesQuotesDetail(salesQuotesDetail);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesQuotesServiceImpl insertSalesQuotesDetail {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg updateSalesQuotesDetail(SalesQuotesDetail salesQuotesDetail) {
        logger.info("SalesQuotesServiceImpl  updateSalesQuotesDetail params "+salesQuotesDetail);
        ResponseMsg responseMsg=new ResponseMsg();
        int result=salesQuotesMapper.updateSalesQuotesDetail(salesQuotesDetail);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesQuotesServiceImpl updateSalesQuotesDetail {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg deleteSalesQuotesDetail(String bjCode) {
        return null;
    }

    @Override
    public ResponseMsg selectSalesQuotesByCode(String doCode) {
        logger.info("SalesQuotesServiceImpl  selectSalesQuotesByCode params{}",doCode);
        ResponseMsg responseMsg=new ResponseMsg();
        SalesQuotes salesQuotes = salesQuotesMapper.selectSalesQuotesByCode(doCode);
        responseMsg.setData(salesQuotes);
        logger.debug("SalesQuotesServiceImpl selectSalesQuotesByCode {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesQuotesDetailByCode(String code) {
        logger.info("SalesQuotesServiceImpl  selectSalesQuotesDetailByCode params{}",code);
        ResponseMsg responseMsg=new ResponseMsg();
        List<SalesQuotesDetail> SalesQuotesDetailList=salesQuotesMapper.selectSalesQuotesDetailByCode(code);
        responseMsg.setData(SalesQuotesDetailList);
        logger.debug("SalesQuotesServiceImpl selectSalesQuotesDetailByCode {}",responseMsg);
        return responseMsg;
    }

    @Transactional
    @Override
    public ResponseMsg check(Map<String, Object> map) {
        logger.info("SalesQuotesServiceImpl check params{}",map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        int result=salesQuotesMapper.check(map);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesQuotesServiceImpl check {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesQuotesByCid(int cid) {
        logger.info("SalesQuotesServiceImpl  selectSalesQuotesByCid params{}",cid);
        ResponseMsg responseMsg=new ResponseMsg();
        List<SalesQuotes> list=salesQuotesMapper.selectSalesQuotesByCid(cid);
        responseMsg.setData(list);
        logger.debug("SalesQuotesServiceImpl selectSalesQuotesByCid {}",responseMsg);
        return responseMsg;
    }
}
